package org.thinkinbunny.financial.data;

import org.thinkinbunny.financial.billentity.BillEntiry;

import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DbHelper extends SQLiteOpenHelper {
	private static final String[][] String = null;
	public static String DbName = "billDB.db";

	private static final String[] typesource = { "打的", "晚餐", "中餐", "早餐", "其他" };
	private static final String[] accountsource = { "支付宝", "现金", "银联" };
	private static final String[] projectsource = { "聚会", "谈判", "约会" };

	public DbHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
		// TODO Auto-generated constructor stub
	}

	public DbHelper(Context context, String name) {
		super(context, name, null, 1);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		ContentValues contentValues = new ContentValues();
		// TODO Auto-generated method stub
		for (int i = 0; i < 6; i++) {
			db.execSQL(TableSchemaInfo.getTableSchema(i));
			switch (i) {
			case 1:
				for (int j = 0; j < typesource.length; j++) {
					contentValues.put("type", typesource[j]);
					db.insert(TableSchemaInfo.typeTable, null, contentValues);
					contentValues.clear();

				}
				break;
			case 2:
				for (int j = 0; j < accountsource.length; j++) {
					contentValues.put("account", accountsource[j]);
					db.insert(TableSchemaInfo.accountTable, null, contentValues);
					contentValues.clear();

				}

				break;
			case 3:
				for (int j = 0; j < projectsource.length; j++) {
					contentValues.put("project", projectsource[j]);
					db.insert(TableSchemaInfo.projectTable, null, contentValues);
					contentValues.clear();

				}
				break;

			default:
				break;
			}
		}

	}

	public static boolean insertIntobill(Context context, BillEntiry billEntiry) {
		ContentValues contentValues = new ContentValues();
		contentValues.put("balance", billEntiry.getBalance());
		contentValues.put("billType", billEntiry.getBillType());
		contentValues.put("accountname", billEntiry.getAccountname());
		contentValues.put("store", billEntiry.getStore());
		contentValues.put("project", billEntiry.getProject());
		contentValues.put("date", billEntiry.getDate());
		contentValues.put("time", billEntiry.getTime());

		DbHelper dbHelper = new DbHelper(context, DbHelper.DbName);
		SQLiteDatabase database = dbHelper.getWritableDatabase();
		if (database.insert("accountInfo", null, contentValues) == -1) {
			contentValues.clear();
			return false;
		}
		database.close();
		try {
			dbHelper.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// to be continue
		contentValues.clear();
		return true;

	}

	public static boolean updateIntobill(Context context, BillEntiry billEntiry) {
		ContentValues contentValues = new ContentValues();

		contentValues.put("balance", billEntiry.getBalance());
		contentValues.put("billType", billEntiry.getBillType());
		contentValues.put("accountname", billEntiry.getAccountname());
		contentValues.put("store", billEntiry.getStore());
		contentValues.put("project", billEntiry.getStore());
		contentValues.put("date", billEntiry.getDate());
		contentValues.put("time", billEntiry.getTime());

		DbHelper dbHelper = new DbHelper(context, DbHelper.DbName);
		SQLiteDatabase database = dbHelper.getWritableDatabase();
		// database.replace("", nullColumnHack, initialValues)

		if (database.update("accountInfo", contentValues, "_id=?",
				new String[] { billEntiry.get_id() + "" }) != 1) {

			return false;
		}
		database.close();
		try {
			dbHelper.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// to be continue
		return true;

	}

	public static void createUserAccount(Context context, String username) {
		try {
			String sqlString = "CREATE TABLE " + username
					+ "accountInfo(_id INTEGER PRIMARY KEY, "
					+ "balance decimal(10,2) DEFAULT 0," + "billType text, "
					+ "accountname text, " + "store text, " + "project text, "
					+ "date text, " + "time time)";
			DbHelper dbHelper = new DbHelper(context, DbHelper.DbName);
			SQLiteDatabase database = dbHelper.getWritableDatabase();
			database.execSQL(sqlString);
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

	public static boolean dropBillById(Context context, int _id) {
		boolean flag = false;

		DbHelper dbHelper = new DbHelper(context, DbHelper.DbName);
		SQLiteDatabase database = dbHelper.getWritableDatabase();
		try {
			if (database.delete("accountInfo", "_id=?",
					new String[] { "" + _id }) != 0) {

				flag = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
			flag = false;
			e.printStackTrace();
		} finally {
			database.close();
		}

		return flag;

	}

	void createDB() {

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

	}

}
